* This is the do file to create "Table D1. Hausman-Wise Test"
set seed 123

use "$path_data/temp/student_unbalance", clear

gen treatment_missing_dummy = 1 if treatment == .
recode treatment_missing_dummy (.=0)
replace treatment = 0 if treatment == .

gen DT_score_pre_missing_0 = 0 if DT_score_pre == .
replace DT_score_pre_missing_0 = DT_score_pre if DT_score_pre != .

gen rosen_pre_missing_0 = 0 if rosen_pre == .
replace rosen_pre_missing_0 = rosen_pre if rosen_pre != .

gen cpcs_pre_missing_0 = 0 if cpcs_pre == .
replace cpcs_pre_missing_0 = cpcs_pre if cpcs_pre != .

gen DT_score_pre_missing_dummy = 1 if DT_score_pre == .
gen rosen_pre_missing_dummy = 1 if rosen_pre == .
gen cpcs_pre_missing_dummy = 1 if cpcs_pre == .
recode DT_score_pre_missing_dummy rosen_pre_missing_dummy cpcs_pre_missing_dummy (.=0)

gen grade_missing_0 = 0 if grade == .
replace grade_missing_0 = grade if grade != .
gen student_gender_missing_0 = 0 if student_gender == .
replace student_gender_missing_0 = grade if student_gender != .
gen grade_missing_dummy = 1 if grade == .
gen student_gender_missing_dummy = 1 if student_gender == .
recode grade_missing_dummy student_gender_missing_dummy (.=0)

*** full sample
wildbootstrap reg attrition treatment treatment_missing_dummy, cluster(school_no) reps(1000)
mat hausman_simple = r(table)
scalar n_hausman_simple = e(N)
mean attrition if treatment == 0
matrix mean_hausman_simple = e(b)

wildbootstrap reg attrition treatment DT_score_pre_missing_0 rosen_pre_missing_0 cpcs_pre_missing_0 DT_score_pre_missing_dummy rosen_pre_missing_dummy cpcs_pre_missing_dummy i.grade_missing_0 student_gender_missing_0 grade_missing_dummy student_gender_missing_dummy treatment_missing_dummy, cluster(school_no) reps(1000)
mat hausman_control = r(table)
scalar n_hausman_control = e(N)

*** subsample
egen DT_score_pre_med = median(DT_score_pre)
gen DT_score_pre_upper50 = 1 if DT_score_pre>DT_score_pre_med
recode DT_score_pre_upper50 (.=0)

wildbootstrap reg attrition treatment DT_score_pre rosen_pre_missing_0 cpcs_pre_missing_0 rosen_pre_missing_dummy cpcs_pre_missing_dummy i.grade_missing_0 student_gender_missing_0 grade_missing_dummy student_gender_missing_dummy treatment_missing_dummy if DT_score_pre_upper50 == 1, cluster(school_no) reps(1000)
mat hausman_upper = r(table)
scalar n_hausman_upper = e(N)
mean attrition if treatment == 0 & DT_score_pre_upper50 == 1
matrix mean_hausman_upper = e(b)


wildbootstrap reg attrition treatment DT_score_pre rosen_pre_missing_0 cpcs_pre_missing_0 rosen_pre_missing_dummy cpcs_pre_missing_dummy i.grade_missing_0 student_gender_missing_0 grade_missing_dummy student_gender_missing_dummy treatment_missing_dummy if DT_score_pre_upper50 == 0, cluster(school_no) reps(1000)
mat hausman_lower = r(table)
scalar n_hausman_lower = e(N)
mean attrition if treatment == 0 & DT_score_pre_upper50 == 0
matrix mean_hausman_lower = e(b)

wildbootstrap reg attrition treatment DT_score_pre_missing_0 rosen_pre_missing_0 cpcs_pre_missing_0 DT_score_pre_missing_dummy rosen_pre_missing_dummy cpcs_pre_missing_dummy student_gender_missing_0 student_gender_missing_dummy treatment_missing_dummy if grade == 4, cluster(school_no) reps(1000)
mat hausman_grade4 = r(table)
scalar n_hausman_grade4 = e(N)
mean attrition if treatment == 0 & grade == 4
matrix mean_hausman_grade4 = e(b)


wildbootstrap reg attrition treatment DT_score_pre_missing_0 rosen_pre_missing_0 cpcs_pre_missing_0 DT_score_pre_missing_dummy rosen_pre_missing_dummy cpcs_pre_missing_dummy student_gender_missing_0 student_gender_missing_dummy treatment_missing_dummy if grade == 2, cluster(school_no) reps(1000)
mat hausman_grade3 = r(table)
scalar n_hausman_grade3 = e(N)
mean attrition if treatment == 0 & grade == 2
matrix mean_hausman_grade3 = e(b)


local specification simple control upper lower grade4 grade3
foreach spec in `specification'{
		if hausman_`spec'[3,1]<=0.01 {
			local star_`spec' %3s "***"
		}
		else if (hausman_`spec'[3,1]>0.01) & (hausman_`spec'[3,1]<=0.05) {
			local star_`spec' %2s "**"
		}
		else if (hausman_`spec'[3,1]>0.05) & (hausman_`spec'[3,1]<=0.10) {
			local star_`spec' %1s "*"
		}
		else {
			local star_`spec'  ""
		}
}

/// Table

tempname hh2
file open `hh2' using "$path_output/hausman_test.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Hausman-Wise Test}" _newline
file write `hh2' "\label{tab:hausman}" _newline
file write `hh2' "\scalebox{0.7}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lcccccc}\toprule" _newline


file write `hh2' "  & Full sample & Full sample & DT Score $>$ median & DT Score $\leq$ median & Grade 4 & Grade 3 \\" _newline

file write `hh2' "  Treatment & " %04.3f (hausman_simple[1,1]) `star_simple' "  & " %04.3f (hausman_control[1,1]) `star_control' " & " %04.3f (hausman_upper[1,1]) `star_upper' " & " %04.3f (hausman_lower[1,1]) `star_lower' " & " %04.3f (hausman_grade4[1,1]) `star_grade4' " & " %04.3f (hausman_grade3[1,1]) `star_grade3' " \\ " _newline
file write `hh2' "    & (" %04.3f (hausman_simple[3,1]) ") & (" %04.3f (hausman_control[3,1]) ") & (" %04.3f (hausman_upper[3,1]) ") & (" %04.3f (hausman_lower[3,1]) ") & (" %04.3f (hausman_grade4[3,1]) ") & (" %04.3f (hausman_grade3[3,1]) ") \\ " _newline
file write `hh2' "  Control Mean & " %04.3f (mean_hausman_simple[1,1]) "  & " %04.3f (mean_hausman_simple[1,1]) " & " %04.3f (mean_hausman_upper[1,1]) " & " %04.3f (mean_hausman_lower[1,1]) " & " %04.3f (mean_hausman_grade4[1,1]) " & " %04.3f (mean_hausman_grade3[1,1])  " \\ " _newline

file write `hh2' "  Control & N & Y & Y & Y & Y & Y \\ " _newline
file write `hh2' "  Observations & " (n_hausman_simple) "  & " (n_hausman_control) " & " (n_hausman_upper) " & " (n_hausman_lower) "  & " (n_hausman_grade4) " & " (n_hausman_grade3) " \\ " _newline


file write `hh2' "\midrule" _newline
file write `hh2' "\end{tabular}" _newline
file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item (a) Dependent variable is the attrition dummy." _newline
file write `hh2' "\item (b) Controls are the students' grade, sex, baseline cognitive and baseline non-cognitive scores." _newline
file write `hh2' "\item (c) Wild clustered bootstrap p-values are reported within parentheses. Clusters are schools at the baseline. There are 34 clusters. " _newline
file write `hh2' "\item (d) $^*$ Significant at 10\% level; $^{**}$ significant at 5\% level; $^{***}$ significant at 1\% level. " _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'
